package hedspi.k55.sm.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.Time;

public class Transaction extends DBmanager {
	public static final int SELL = 1;
	public static final int BUY = 2;

	private int id;
	private int type;
	private String time;
	private String stock_symbol;
	private int shares;
	private double price;
	private double commission;
	private String note;

	public Transaction(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
	}

	// zend update(15/4): constructor and get method
	public Transaction(Context context, int type, String stock_symbol,
			String time, int shares, double price, double commission,
			String notes) {
		super(context);
		this.type = type;
		this.stock_symbol = stock_symbol;
		this.time = time;
		this.shares = shares;
		this.price = price;
		this.commission = commission;
		this.note = new String(notes);
	}

	public int add() {
		if (this.id > 0) {
			return -1;
		}
		// ktra stock symbol
		if(isExist(TABLE_STOCK, _SYMBOL, this.stock_symbol)){
			Stock stock = new Stock(context,this.stock_symbol);
			stock.init(); // lay thong tin tu CSDL
			int old_my_shares = Integer.parseInt(stock.getMyShares());
			double old_avg_price = Double.parseDouble(stock.getAvgPrice());
			int new_my_share = old_my_shares + this.shares;
			double new_avg_price = (old_avg_price*old_my_shares + this.shares*this.price) / new_my_share ;
			stock.setMyShares(new_my_share + "");
			stock.setAvgPrice(new_avg_price + "");
			stock.update();
		}
		else{
			Stock stock = new Stock(context,this.stock_symbol);
			stock.refresh(); // lay thong tin tu internet
			stock.setAvgPrice(this.price + "");
			stock.setMyShares(this.shares + "");
			stock.update();
			
		}
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues value = new ContentValues();

		value.put(_TYPE, this.type);
		value.put("symbol", this.stock_symbol);
		value.put("time", this.time.toString());
		value.put("shares", this.shares);
		value.put("pricein", this.price);
		value.put("commission", this.commission);
		value.put("note", this.note);

		int id = (int) db.insert(TABLE_TRANSACTION, null, value);
		if (id != -1) {
			this.id = id;
		}
		db.close();
		return id;
	}

	public boolean update() {
		if (this.id <= 0) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues value = new ContentValues();

		value.put("type", this.type);
		value.put("symbol", this.stock_symbol);
		value.put("time", this.time.toString());
		value.put("shares", this.shares);
		value.put("pricein", this.price);
		value.put("commission", this.commission);
		value.put("note", this.note);

		int affect = (int) db.update(TABLE_TRANSACTION, value, "id = ?",
				new String[] { this.id + "" });
		if (affect != 1) {
			return false;
		}
		return true;
	}

	public int getType() {
		return this.type;
	}

	public String getStockSymbol() {
		return this.stock_symbol;
	}

	public String getTime() {
		return this.time;
	}

	public int getShares() {
		return this.shares;
	}

	public double getPrice() {
		return this.price;
	}

	public double getCommission() {
		return this.commission;
	}

	public String getNotes() {
		return this.note;
	}

	// zend update(16/4): update set method
	public boolean setType(int type) {
		if (type == 1 || type == 2) {
			this.type = type;
			return true;
		}
		return false;
	}

	public boolean setID(int id) {
		if (id > 0) {
			this.id = id;
			return true;
		}
		return false;
	}

	public boolean setTime(Time time) {
		this.time = time.toString();
		return true;
	}

	public boolean setStockSymbol(String symbol) {
		this.stock_symbol = symbol;
		return true;
		// return false when symbol.length > ??
	}

	public boolean setShares(int share) {
		if (share > 0) {
			this.shares = share;
			return true;
		}
		return false;
	}

	public boolean setPrice(double price) {
		if (price > 0) {
			this.price = price;
			return true;
		}
		return false;
	}

	public boolean setCommission(Double commission) {
		if (commission > 0) {
			this.commission = commission;
			return true;
		}
		return false;
	}

	public boolean setNotes(String notes) {
		this.note = new String(notes);
		return true;
	}

	// zend update(16/4): complete method getAmount()
	public Double getAmount() {
		return (this.price * this.shares);
	}
	/*
	 * wrong // zend update(16/4): update method(add,update) do with database
	 * public boolean create(Transaction transaction) { DBmanager myDB = new
	 * DBmanager(null); SQLiteDatabase db = myDB.getWritableDatabase();
	 * ContentValues value = new ContentValues();
	 * 
	 * value.put(DBmanager._TYPE, this.type); value.put(DBmanager._STOCK_ID,
	 * this.stock_id); value.put(DBmanager._TIME, this.time.toString());
	 * value.put(DBmanager._SHARES, this.shares); value.put(DBmanager._PRICE_IN,
	 * this.price); value.put(DBmanager._COMMISSION, this.commission);
	 * value.put(DBmanager._NOTES, this.note);
	 * 
	 * // insert put back id of row ?? check again this.id = (int)
	 * db.insert(DBmanager.TABLE_TRANSACTION, null, value);
	 * 
	 * db.close(); return true; }
	 * 
	 * public boolean update() { if (this.id == null) { return false; } else {
	 * DBmanager myDB = new DBmanager(null); SQLiteDatabase db =
	 * myDB.getWritableDatabase(); ContentValues value = new ContentValues();
	 * 
	 * value.put(DBmanager._TYPE, this.type); value.put(DBmanager._STOCK_ID,
	 * this.stock_id); value.put(DBmanager._TIME, this.time.toString());
	 * value.put(DBmanager._SHARES, this.shares); value.put(DBmanager._PRICE_IN,
	 * this.price); value.put(DBmanager._COMMISSION, this.commission);
	 * value.put(DBmanager._NOTES, this.note);
	 * db.update(DBmanager.TABLE_TRANSACTION, value, DBmanager._ID, new String[]
	 * { this.id.toString() }); return true; } }
	 */

}
